package com.visionet.school.app.web.service.wechat;

import com.visionet.school.app.web.entity.wechat.Menu;
import com.visionet.school.app.web.repository.wechat.MenuRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * Created by hxpeng on 2017/3/21.
 */
public class MenuService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MenuRepository menuRepository;

    /*
     *
     * 获取所有菜单
     */
    public List<Menu> findCmWxmenusList() {
        return menuRepository.findCmWxmenusList();
    }

    /*
     *
     * 获取某个菜单信息
     */
//    public Menu findOneCmWxmenusById(String id) {
//        return menuRepository.findOneCmWxmenusById(id);
//    }

    /*
     * 删除
     */
    public boolean deleteCmWxmenus(List<Menu> cmWxmenusVoList) {
        Menu cmWxmenus = null;
        for (Menu cmWxmenusVo : cmWxmenusVoList) {
            cmWxmenus = menuRepository.findOne(cmWxmenusVo.getId());
            menuRepository.delete(cmWxmenus);
        }
        return true;
    }

    /*
     *
     * 更新和添加新菜单
     */
    public boolean updateCmWxmenus(Menu cmWxmenus){
        if(null==cmWxmenus){
            return false;
        }
        if(null==cmWxmenus.getId()){
            menuRepository.save(cmWxmenus);
            return true;
        }
        Menu cmWxmenusOld = menuRepository.findOne(cmWxmenus.getId());
        BeanUtils.copyProperties(cmWxmenus, cmWxmenusOld);
        menuRepository.save(cmWxmenusOld);
        return true;
    }

    /*
     *
     * 菜单类型（现取最新3个）
     */
    public List<Menu> findShowParent(){
        return menuRepository.findShowParent();
    }

    /*
     *
     * 获取该父菜单下的子菜单 5个
     */
    public List<Menu> findChildMenus(String parentid){
        return menuRepository.findChildMenus(parentid);
    }


}
